//
//  RBSearchResultLimitCell.m
//  zhongzhenglu
//
//  Created by 马军凯 on 2023/5/4.
//

#import "RBSearchResultLimitCell.h"
#import "RBSearchResultResponse.h"
#import "RBSearchGoodsProgressView.h"

@interface RBSearchResultLimitCell ()
//商品图片
@property (nonatomic, strong) UIImageView *goodsImgView;
//
@property (nonatomic, strong) UIView *rightContainerV;
//商品名称
@property (nonatomic, strong) UILabel *goodsNameL;
//抢购
@property (nonatomic, strong) RBSearchGoodsProgressView *progressView;
//剩余
@property (nonatomic, strong) UILabel *stockL;
//价格
@property (nonatomic, strong) UILabel *priceL;
//原价
@property (nonatomic, strong) UILabel *originPriceLabel;
//抢购
@property (nonatomic, strong) UILabel *buyBtn;

@end

@implementation RBSearchResultLimitCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self buyBtn];
    }
    return self;
}

//赋值
- (void)setCellModel:(RBCellModel *)cellModel {
    RBSearchResultModel *model = cellModel.cellData;
    //图片
    [self.goodsImgView setImageWithUrl:model.picurl placeHolder:nil];
    //商品名称
    self.goodsNameL.text = model.good_name;
    //进度条
    CGFloat totalNum = [model.available_sale floatValue];
    CGFloat sellNum = totalNum - [model.exchange_num floatValue];
    CGFloat percent = sellNum / totalNum;
    if (percent < 0.01 && percent != 0) {
        percent = 0.01;
    }
    if (percent > 0.5) {
        self.progressView.titleColor = [UIColor whiteColor];
    } else {
        self.progressView.titleColor = [RBThemeConfig tabBarSelectColor];
    }
    self.progressView.percent = percent;
    //售价
    self.priceL.attributedText = [NSString changeSectionFontWithString:[NSString stringWithFormat:@"¥%@", model.current_price] font:[UIFont systemFontOfSize:11 weight:UIFontWeightSemibold] from:0 lenth:1];
    //原价
    self.originPriceLabel.attributedText = [NSString stringMidLine:[NSString stringWithFormat:@"¥%@", model.shop_price]];
    //剩余
    self.stockL.text = [NSString stringWithFormat:@"剩余%@", model.exchange_num];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

#pragma mark - Getter
//商品图片
- (UIImageView *)goodsImgView {
    if (!_goodsImgView) {
        self.goodsImgView = ({
            UIImageView *view = UIImageView.new;
            view.contentMode = UIViewContentModeScaleAspectFill;
            [view rbSetCornerRadius:Zoom(4)];
            [self.contentView addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(12);
                make.top.mas_equalTo(13);
                make.width.height.mas_equalTo(97);
            }];
            view;
        });
    }
    return _goodsImgView;
}
//
- (UIView *)rightContainerV {
    if (!_rightContainerV) {
        self.rightContainerV = ({
            UIView *view = UIView.new;
            [self.contentView addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(12);
                make.right.mas_equalTo(-13);
                make.bottom.mas_equalTo(-12);
                make.left.mas_equalTo(self.goodsImgView.mas_right).offset(14);
            }];
            
            view;
        });
        
    }
    return _rightContainerV;
}
//商品名称
- (UILabel *)goodsNameL {
    if (!_goodsNameL) {
        self.goodsNameL = ({
            UILabel *view = [RanTool labelWithFontSize:16 weight:1 color:[RBThemeConfig textBlackColor] align:0 text:@""];
            [self.rightContainerV addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(self.rightContainerV.mas_top).offset(0);
                make.left.right.mas_equalTo(0);
            }];
            view;
        });
    }
    return _goodsNameL;
}
//进度
- (RBSearchGoodsProgressView *)progressView {
    if (!_progressView) {
        self.progressView = ({
            RBSearchGoodsProgressView *view = RBSearchGoodsProgressView.new;
            view.itemW = 173;
            view.itemH = 14;
            [self.rightContainerV addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(0);
                make.top.mas_equalTo(self.goodsNameL.mas_bottom).offset(10);
                make.width.mas_equalTo(view.itemW);
                make.height.mas_equalTo(view.itemH);
            }];
            view;
        });
    }
    return _progressView;
}
//售价
- (UILabel *)priceL {
    if (!_priceL) {
        self.priceL = ({
            UILabel *view = UILabel.new;
            view.textColor = [UIColor LC_hex:@"#F04D27"];
            view.font = [UIFont systemFontOfSize:15 weight:UIFontWeightSemibold];
            [self.rightContainerV addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(0);
                make.top.mas_equalTo(self.progressView.mas_bottom).offset(20);
            }];
            view;
        });
    }
    return _priceL;
}
//原价
- (UILabel *)originPriceLabel {
    if (!_originPriceLabel) {
        self.originPriceLabel = ({
            UILabel *view = UILabel.new;
            view.textColor = [UIColor LC_hex:@"#999999"];
            view.font = [UIFont systemFontOfSize:12];
            [self.rightContainerV addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(0);
                make.top.mas_equalTo(self.priceL.mas_bottom).offset(13);
                make.bottom.mas_equalTo(self.rightContainerV.mas_bottom).offset(0);
            }];
            view;
        });
    }
    return _originPriceLabel;
}
//剩余
- (UILabel *)stockL {
    if (!_stockL) {
        self.stockL = ({
            UILabel *view = [RanTool labelWithFontSize:12 weight:1 color:[RBThemeConfig textGrayColor] align:0 text:@""];
            [self.rightContainerV addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(self.originPriceLabel.mas_right).offset(8);
                make.centerY.mas_equalTo(self.originPriceLabel);
            }];
            view;
        });
    }
    return _stockL;
}

//抢购
- (UILabel *)buyBtn {
    if (!_buyBtn) {
        self.buyBtn = ({
            UILabel *view = [RanTool labelWithFontSize:13 weight:0 color:UIColor.whiteColor align:1 text:@"抢购" bgColor:@"#E83922" radius:3];
            [self.rightContainerV addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.mas_equalTo(0);
                make.bottom.mas_equalTo(self.originPriceLabel.mas_top).offset(0);
                make.width.mas_equalTo(47);
                make.height.mas_equalTo(24);
            }];
            view;
        });
    }
    return _buyBtn;
}

@end
